<template>
  <div class="lb-auth">
    <lb-title></lb-title>
    <div class="page-main search-form">
      <el-row class="page-top-operate">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="$router.push('/editAuth')"
          >新增授权</el-button
        >
      </el-row>
      <div class="page-top-operate form-search">
        <el-form @submit.native.prevent :inline="true" :model="pageForm">
          <el-form-item label="代理商">
            <el-select
              @change="getTableDataList(1)"
              v-model="pageForm.agent_id"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.user_name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="小程序名称">
            <el-input
              v-model="pageForm.mini_name"
              placeholder="请输入小程序名称"
            ></el-input>
          </el-form-item>
          <lb-button type="primary" @click="getTableDataList(1)">{{
            $t('action.search')
          }}</lb-button>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80px"></el-table-column>
        <el-table-column prop="mini_name" label="小程序名"></el-table-column>
        <el-table-column
          prop="modular_id"
          label="小程序id"
          width="80px"
        ></el-table-column>
        <el-table-column prop="number" label="创建名片数量"></el-table-column>
        <el-table-column prop="copyright" label="版权"></el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="end_time" label="到期时间">
          <template slot-scope="scope">
            <p>{{ scope.row.end_time | handleTime(1) }}</p>
            <p>{{ scope.row.end_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="agent_name" label="代理商"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <el-button
                type="primary"
                size="mini"
                plain
                @click="editAuthInfo('edit', scope.row.id)"
                >{{ $t('action.edit') }}</el-button
              >
              <!-- <el-button
              type="danger"
              size="mini"
              plain
              @click="deleteAuthInfo(scope.row.id)"
            >{{$t('action.delete')}}</el-button> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :page="pageForm.page"
        :pageSize="pageForm.page_count"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  name: 'auth',
  data () {
    return {
      options: [],
      loading: false,
      pageForm: {
        page: 1,
        page_count: 10,
        mini_name: '',
        agent_id: ''
      },
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getAgentAuthSelect()
    this.getTableDataList()
  },
  methods: {
    async getAgentAuthSelect (name) {
      let { data } = await this.$api.agentAuthSelect({
        name
      })
      this.options = data
    },
    getTableDataList (flag) {
      this.loading = true
      if (flag) this.pageForm.page = 1
      let { pageForm } = this
      this.$api.getAuthList(pageForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    editAuthInfo (type, id) {
      this.$router.push('/editAuth?id=' + id)
    },
    deleteAuthInfo (id) {
      this.$confirm(
        this.$t('tips.confirmDelete') + '删除后将无法恢复！',
        this.$t('tips.reminder'),
        {
          confirmButtonText: this.$t('action.comfirm'),
          cancelButtonText: this.$t('action.cancel'),
          type: 'warning'
        }
      ).then(() => {
        this.$api.delAuthInfo({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.getTableDataList()
          }
        })
      })
    },
    handleSizeChange (val) {
      this.pageForm.page_count = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.pageForm.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-auth {
  width: 100%;
}
</style>
